//
//  RBSearchRankView.m
//  zhongzhenglu
//
//  Created by XL on 2022/7/11.
//

#import "RBSearchRankView.h"
#import "RBDefine.h"
#import "UIScrollView+RBAddHorizontal.h"
#import <Masonry.h>
#import "UIView+RBAdd.h"
#import "RBGradientView.h"
#import <UIColor+YYAdd.h>
#import "RBThemeConfig.h"
#import "RBSearchRankImgItemView.h"
#import "RBSearchRankTextItemView.h"
#import "RBGoodsDetailVC.h"

@interface RBSearchRankView()

@property(nonatomic, strong)UIScrollView *scrollView;

@end

@implementation RBSearchRankView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        
    }
    return self;
}

- (void)setData:(RBSearchModel *)data {
    _data = data;
    NSMutableArray *countArray = [NSMutableArray array];
    CGFloat maxHeight = 0;
    if (data.browse_top.count > 0) {
        [countArray addObject:@"人气排行"];
        CGFloat totalHeight = 0;
        if (data.browse_top.count > 3) {
            totalHeight = Zoom(50) + Zoom(59) * 3 + Zoom(16) * (data.browse_top.count - 1) + Zoom(36) * (data.browse_top.count - 3);
        } else {
            totalHeight = Zoom(50) + Zoom(59) * data.browse_top.count + Zoom(16) * (data.browse_top.count - 1);
        }
        if (totalHeight > maxHeight) {
            maxHeight = totalHeight;
        }
    }
    if (data.sell_top.count > 0) {
        [countArray addObject:@"销量排行"];
        CGFloat totalHeight = 0;
        if (data.sell_top.count > 3) {
            totalHeight = Zoom(50) + Zoom(59) * 3 + Zoom(16) * (data.sell_top.count - 1) + Zoom(36) * (data.sell_top.count - 3);
        } else {
            totalHeight = Zoom(50) + Zoom(59) * data.sell_top.count + Zoom(16) * (data.sell_top.count - 1);
        }
        if (totalHeight > maxHeight) {
            maxHeight = totalHeight;
        }
    }
    self.suggestHeight = maxHeight;
    [self.scrollView h_rbSetContentHeight:self.suggestHeight];
    
    for (NSInteger i = 0; i < countArray.count; i++) {
        UIView *containerView = UIView.new;
        RBGradientView *bgView = [RBGradientView gradientViewWithColors:@[[UIColor colorWithHexString:@"#CADCFF"], [UIColor colorWithHexString:@"#F2F2F2"]] locations:@[@0, @1] vertical:YES];
        [containerView addSubview:bgView];
        [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
        }];
        
        [containerView view_addView:({
            UILabel *titleL = UILabel.new;
            titleL.font = [UIFont boldSystemFontOfSize:Zoom(16)];
            titleL.textColor = [RBThemeConfig textBlackColor];
            titleL.text = countArray[i];
            titleL;
        }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:Zoom(18) bottom:CGFLOAT_MIN left:Zoom(14) right:Zoom(14) centerX:0];
        
        
        if ([countArray[i] isEqualToString:@"人气排行"]) {
            for (NSInteger i = 0; i < data.browse_top.count; i++) {
                RBSearchBrowseTopModel *model = data.browse_top[i];
                if (i < 3) {
                    [containerView view_addView:({
                        RBSearchRankImgItemView *view = [RBSearchRankImgItemView view];
                        view.backgroundColor = UIColor.clearColor;
                        view.browseModel = model;
                        
                        view.rbExData = model;
                        [view rbAddTapGesWithTarget:self action:@selector(clickBrowseTopAction:)];
                        
                        view;
                    }) withHeight:Zoom(59) width:CGFLOAT_MIN top:Zoom(16) bottom:CGFLOAT_MIN left:Zoom(14) right:Zoom(14) centerX:0];
                } else {
                    [containerView view_addView:({
                        RBSearchRankTextItemView *view = [RBSearchRankTextItemView view];
                        view.backgroundColor = UIColor.clearColor;
                        view.browseModel = model;
                        view.numL.text = [NSString stringWithFormat:@"%zd", (i + 1)];
                        view.rbExData = model;
                        [view rbAddTapGesWithTarget:self action:@selector(clickBrowseTopAction:)];
                        
                        view;
                    }) withHeight:Zoom(36) width:CGFLOAT_MIN top:Zoom(12) bottom:CGFLOAT_MIN left:Zoom(14) right:Zoom(14) centerX:0];
                }
            }
        } else {
            for (NSInteger i = 0; i < data.sell_top.count; i++) {
                RBSearchSellTopModel *model = data.sell_top[i];
                if (i < 3) {
                    [containerView view_addView:({
                        RBSearchRankImgItemView *view = [RBSearchRankImgItemView view];
                        view.backgroundColor = UIColor.clearColor;
                        view.sellModel = model;
                        view.rbExData = model;
                        [view rbAddTapGesWithTarget:self action:@selector(clickSellTopAction:)];
                        view;
                    }) withHeight:Zoom(59) width:CGFLOAT_MIN top:Zoom(16) bottom:CGFLOAT_MIN left:Zoom(14) right:Zoom(14) centerX:0];
                } else {
                    [containerView view_addView:({
                        RBSearchRankTextItemView *view = [RBSearchRankTextItemView view];
                        view.backgroundColor = UIColor.clearColor;
                        view.sellModel = model;
                        view.numL.text = [NSString stringWithFormat:@"%zd", (i + 1)];
                        view.rbExData = model;
                        [view rbAddTapGesWithTarget:self action:@selector(clickSellTopAction:)];
                        view;
                    }) withHeight:Zoom(36) width:CGFLOAT_MIN top:Zoom(12) bottom:CGFLOAT_MIN left:Zoom(14) right:Zoom(14) centerX:0];
                }
            }
        }
        
        [self.scrollView h_addView:containerView withHeight:CGFLOAT_MIN width:Zoom(259) top:0 bottom:0 left:12 right:12 centerY:0];
    }
}

//人气排行
- (void)clickBrowseTopAction:(UITapGestureRecognizer *)tap {
    RBSearchBrowseTopModel *model = tap.view.rbExData;
    RBGoodsDetailVC *vc = RBGoodsDetailVC.new;
    vc.id = model.id;
    
    [self.viewController.navigationController pushViewController:vc animated:YES];
}
//销量排行
- (void)clickSellTopAction:(UITapGestureRecognizer *)tap {
    RBSearchSellTopModel *model = tap.view.rbExData;
    RBGoodsDetailVC *vc = RBGoodsDetailVC.new;
    vc.id = model.goods_id;
    
    [self.viewController.navigationController pushViewController:vc animated:YES];
}

#pragma mark - Getter

- (UIScrollView *)scrollView
{
    if (!_scrollView)
    {
        self.scrollView = ({
            UIScrollView *view = UIScrollView.new;
            view.showsVerticalScrollIndicator = NO;
            view.showsHorizontalScrollIndicator = NO;
            [self addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.mas_equalTo(0);
            }];
            view;
        });
    }
    return _scrollView;
}

@end
